<template>
	<div class="type-nav">
		<div class="container">
			<div @mouseleave="leaveShow" @mouseenter="enterShow">
				<h2 class="all">全部商品分类</h2>
				<transition name="sort">
					<div class="sort" v-show="isShow">
						<div class="all-sort-list2">
							<div
								@click="goSearch($event)"
								class="item tabLabel"
								:class="{ itemHove: itemIndex === index }"
								v-for="(value, index) in categoryListObj"
								:key="index"
							>
								<h3 @mouseenter="hoveAddClass(index)">
									<a
										:style="{ color: itemIndex === index ? '#fff' : '#333' }"
										:data-categoryName="value.categoryName"
										:data-category1Id="value.categoryId"
										>{{ value.categoryName }}</a
									>
								</h3>
								<div
									class="item-list clearfix"
									:style="{ display: itemIndex === index ? 'block' : 'none' }"
								>
									<div class="subitem">
										<dl class="fore" v-for="value2 in value.categoryChild" :key="value2.categoryId">
											<dt>
												<a
													:data-categoryName="value2.categoryName"
													:data-category2Id="value.categoryId"
													>{{ value2.categoryName }}</a
												>
											</dt>
											<dd>
												<em v-for="value3 in value2.categoryChild" :key="value3.categoryId">
													<a
														:data-categoryName="value3.categoryName"
														:data-category3Id="value.categoryId"
														>{{ value3.categoryName }}</a
													>
												</em>
											</dd>
										</dl>
									</div>
								</div>
							</div>
						</div>
					</div>
				</transition>
			</div>
			<nav class="nav">
				<a href="javascript:;">服装城</a>
				<a href="javascript:;">美妆馆</a>
				<a href="javascript:;">尚品汇超市</a>
				<a href="javascript:;">全球购</a>
				<a href="javascript:;">闪购</a>
				<a href="javascript:;">团购</a>
				<a href="javascript:;">有趣</a>
				<a href="javascript:;">秒杀</a>
			</nav>
		</div>
	</div>
</template>

<script>
import { mapState } from 'vuex';
import throttle from 'lodash/throttle';
export default {
	name: 'TypeNav',
	data() {
		return {
			// 是否显示一级路由信息
			isShow: false,
			// 序号
			itemIndex: -1,
		};
	},
	computed: {
		// vuex数据
		...mapState('home', ['categoryListObj']),
	},
	methods: {
		// 鼠标进入高亮(节流进入)
		hoveAddClass: throttle(function (index) {
			this.itemIndex = index;
		}, 20),
		// 鼠标离开移除高亮
		leaveShow: throttle(function () {
			this.itemIndex = -1;
			// 判断是否为home路由
			if (this.$route.path !== '/home') {
				this.isShow = false;
			}
		}, 20),
		// 鼠标移入显示一级
		enterShow() {
			this.isShow = true;
		},

		// 路由跳转
		goSearch(event) {
			// 对象格式 打印出元素中所有自定义属性
			let isData = event.target.dataset;
			let { categoryname, category1id, category2id, category3id } = isData;
			// 跳转地址 ==> search
			let location = { name: 'search' };
			// 路由信息
			let query = { categoryName: categoryname };
			// 判断点击的是否为a标签(检查属性中是否有自定义属性)
			if (categoryname) {
				// 判断a标签为几级路由
				if (category1id) {
					query.category1Id = category1id;
				} else if (category2id) {
					query.category2Id = category2id;
				} else {
					query.category3Id = category3id;
				}
				//判断：如果路由跳转的时候，带有params参数，捎带脚传递过去
				if (this.$route.params) {
					location.params = this.$route.params;
					//动态给location配置对象添加query属性
					location.query = query;
					//路由跳转
					this.$router.push(location);
				}
			}
		},
	},
	mounted() {
		if (this.$route.path !== '/home') {
			this.isShow = false;
		} else {
			this.isShow = true;
		}
	},
};
</script>

<style scoped>
.type-nav {
	border-bottom: 2px solid #e1251b;
}
.type-nav .container {
	width: 1200px;
	margin: 0 auto;
	display: flex;
	position: relative;
}
.type-nav .container .all {
	width: 210px;
	height: 45px;
	background-color: #e1251b;
	line-height: 45px;
	text-align: center;
	color: #fff;
	font-size: 14px;
	font-weight: bold;
}
.type-nav .container .nav a {
	height: 45px;
	margin: 0 22px;
	line-height: 45px;
	font-size: 16px;
	color: #333;
}
.type-nav .container .sort {
	left: 0;
	top: 47px;
	width: 210px;
	height: 461px;
	position: absolute;
	background: #fafafa;
	z-index: 999;
}
.itemHove {
	background-color: #e1251b;
	padding-left: 10px;
}
.type-nav .container .sort .all-sort-list2 .item h3 {
	line-height: 30px;
	font-size: 14px;
	font-weight: 400;
	overflow: hidden;
	padding: 0 20px;
	margin: 0;
}
.type-nav .container .sort .all-sort-list2 .item h3 a {
	color: #333;
}
.type-nav .container .sort .all-sort-list2 .item .item-list {
	display: none;
	position: absolute;
	width: 734px;
	min-height: 460px;
	background: #f7f7f7;
	left: 210px;
	border: 1px solid #ddd;
	top: 0;
	z-index: 9999 !important;
}
.type-nav .container .sort .all-sort-list2 .item .item-list .subitem {
	float: left;
	width: 650px;
	padding: 0 4px 0 8px;
}
.type-nav .container .sort .all-sort-list2 .item .item-list .subitem dl {
	border-top: 1px solid #eee;
	padding: 6px 0;
	overflow: hidden;
	zoom: 1;
}
.type-nav .container .sort .all-sort-list2 .item .item-list .subitem dl.fore {
	border-top: 0;
}
.type-nav .container .sort .all-sort-list2 .item .item-list .subitem dl dt {
	float: left;
	width: 54px;
	line-height: 22px;
	text-align: right;
	padding: 3px 6px 0 0;
	font-weight: 700;
}
.type-nav .container .sort .all-sort-list2 .item .item-list .subitem dl dd {
	float: left;
	width: 415px;
	padding: 3px 0 0;
	overflow: hidden;
}
.type-nav .container .sort .all-sort-list2 .item .item-list .subitem dl dd em {
	float: left;
	height: 14px;
	line-height: 14px;
	padding: 0 8px;
	margin-top: 5px;
	border-left: 1px solid #ccc;
}
/* 动画开始时和动画结束时的状态 */
.sort-enter,
.sort-leave-to {
	overflow: hidden;
	height: 0px !important;
	opacity: 0;
}
.sort-leave-active,
.sort-enter-active {
	transition: all 0.2s;
}
/* 动画结束时和动画开始时的状态 */
.sort-leave,
.sort-enter-to {
	overflow: hidden;
	height: 461px !important;
	opacity: 1;
}
.tabLabel {
	transition: all 0.2s linear;
}
</style>
